package com.srlg.batch.user;

import com.srlg.batch.model.entity.User;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@Configuration
public class BatchConfiguration {

    @Bean
    public Job importUserJob(JobRepository jobRepository, Step step1, JobCompletionNotificationListener listener) {
        return new JobBuilder("importUserJob", jobRepository)
                .listener(listener)
                .start(step1).build();
    }

    @Bean("step1")
    public Step step_1(JobRepository jobRepository, DataSourceTransactionManager transactionManager,
                       FlatFileItemReader<User> userFlatFileItemReader, UserItemProcessor processor, JdbcBatchItemWriter<User> userJdbcBatchItemWriter) {

        return new StepBuilder("step1", jobRepository)
                .<User, User>chunk(3, transactionManager)
                .reader(userFlatFileItemReader)
                .processor(processor)
                .writer(userJdbcBatchItemWriter)
                .build();
    }
}
